﻿Imports System.Text
Imports FTTK.Entities

Namespace FTTK.Utilities
    Friend NotInheritable Class RequestUrlHelper
        Private Sub New()

        End Sub

        Private Shared _BaseUrl As String = "http://api.fotolia.com/Rest/1/"

        Public Shared Function TestRequestUrl() As String
            Return String.Concat(_BaseUrl, "main/test")
        End Function

        Public Shared Function GetDataRequestUrl() As String
            Return String.Concat(_BaseUrl, "main/getData")
        End Function

        Public Shared Function GetRepresentativeCategoriesUrl(ByVal languageId As Integer,
                                                              Optional ByVal idCategoryParent As Integer? = Nothing) As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("search/getCategories1?")
            AppendParameter(strBuild, "language_id", languageId)
            AppendParameter(strBuild, "id", Function() idCategoryParent.Value, Function() idCategoryParent.HasValue)
            Return strBuild.ToString()
        End Function

        Public Shared Function GetConceptualCategoriesUrl(ByVal languageId As Integer,
                                                          Optional ByVal idCategoryParent As Integer? = Nothing) As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("search/getCategories2?")
            AppendParameter(strBuild, "language_id", languageId)
            AppendParameter(strBuild, "id", Function() idCategoryParent.Value, Function() idCategoryParent.HasValue)
            Return strBuild.ToString()
        End Function

        Public Shared Function GetTagsUrl(ByVal languageId As Integer,
                                          ByVal tagType As Entities.TagType) As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("search/getTags?")
            AppendParameter(strBuild, "language_id", languageId)
            AppendParameter(strBuild, "type", TagTypes.GetTypeString(tagType))
            Return strBuild.ToString()
        End Function

        Public Shared Function GetGalleriesUrl(ByVal languageId As Integer, thumbnailSize As GalleryThumbnailSize) As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("search/getGalleries?")
            AppendParameter(strBuild, "language_id", languageId)
            AppendParameter(strBuild, "thumbnail_size", FTTK.Entities.Rest.GalleryThumbnailSize.FromEntitySize(thumbnailSize))
            Return strBuild.ToString()
        End Function

        Shared Function GetSeasonalGalleriesUrl(languageId As Integer, thumbnailSize As GalleryThumbnailSize, themeId As Integer?) As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("search/getSeasonalGalleries?")
            AppendParameter(strBuild, "language_id", languageId)
            AppendParameter(strBuild, "thumbnail_size", FTTK.Entities.Rest.GalleryThumbnailSize.FromEntitySize(thumbnailSize))
            If themeId.HasValue Then
                AppendParameter(strBuild, "theme_id", themeId.Value)
            End If
            Return strBuild.ToString()
        End Function

        Public Shared Function GetHomePageImagesUrl(ByVal languageId As Integer) As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("media/getHomePageImages?")
            AppendParameter(strBuild, "language_id", languageId)
            Return strBuild.ToString()
        End Function

        Public Shared Function GetCountriesUrl(ByVal languageId As Integer) As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("search/getCountries?")
            AppendParameter(strBuild, "language_id", languageId)
            Return strBuild.ToString()
        End Function

        Public Shared Function GetSearchResultsUrl(ByVal request As SearchRequest, columns As SearchResultsColumns) As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("search/getSearchResults?")
            AppendParameter(strBuild, "search_parameters[language_id]", request.LanguageID)
            AppendParameter(strBuild, "search_parameters[limit]", request.MaximumResult)
            AppendParameter(strBuild, "search_parameters[offset]", request.Offset)
            AppendParameter(strBuild, "search_parameters[color_name]", request.ColorName, Function() Not String.IsNullOrEmpty(request.ColorName))
            AppendParameter(strBuild, "search_parameters[cat2_id]", Function() request.ConceptualCategoryId.Value, Function() request.ConceptualCategoryId.HasValue)
            AppendParameter(strBuild, "search_parameters[country_id]", Function() request.CountryId.Value, Function() request.CountryId.HasValue)
            AppendParameter(strBuild, "search_parameters[creator_id]", Function() request.CountryId.Value, Function() request.CreatorId.HasValue)
            AppendParameter(strBuild, "search_parameters[detail_level]", "1", Function() request.DetailLevel = SearchDetailLevel.Extended)
            AppendParameter(strBuild, "search_parameters[gallery_id]", request.GalleryId, Function() Not String.IsNullOrWhiteSpace(request.GalleryId))
            AppendParameter(strBuild, "search_parameters[media_id]", Function() request.MediaId.Value, Function() request.MediaId.HasValue)
            AppendParameter(strBuild, "search_parameters[model_id]", Function() request.ModelId.Value, Function() request.ModelId.HasValue)
            AppendParameter(strBuild, "search_parameters[cat1_id]", Function() request.RepresentativeCategoryId.Value, Function() request.RepresentativeCategoryId.HasValue)
            AppendParameter(strBuild, "search_parameters[order]", FTTK.Entities.Rest.SearchResultsOrder.FromEntityOrder(request.ResultsOrderCriteria), Function() request.ResultsOrderCriteria <> SearchResultsOrder.Relevance)
            AppendParameter(strBuild, "search_parameters[serie_id]", Function() request.SerieId.Value, Function() request.SerieId.HasValue)
            AppendParameter(strBuild, "search_parameters[similia_id]", Function() request.SimiliaId.Value, Function() request.SimiliaId.HasValue)
            AppendParameter(strBuild, "search_parameters[thumbnail_size]", FTTK.Entities.Rest.SearchThumbnailSize.FromEntitySize(request.ThumbnailSize), Function() request.ThumbnailSize <> SearchThumbnailSize.Size110)
            AppendParameter(strBuild, "search_parameters[words]", request.Words, Function() Not String.IsNullOrEmpty(request.Words))
            If request.Filters IsNot Nothing Then
                AppendParameter(strBuild, "search_parameters[filters][content_type:illustration]", "1", Function() request.Filters.ContentType = SearchResultsContentType.Illustration And request.Filters.ContentType <> SearchResultsContentType.All)
                AppendParameter(strBuild, "search_parameters[filters][content_type:photo]", "1", Function() request.Filters.ContentType = SearchResultsContentType.Photo And request.Filters.ContentType <> SearchResultsContentType.All)
                AppendParameter(strBuild, "search_parameters[filters][content_type:video]", "1", Function() request.Filters.ContentType = SearchResultsContentType.Video And request.Filters.ContentType <> SearchResultsContentType.All)
                AppendParameter(strBuild, "search_parameters[filters][content_type:vector]", "1", Function() request.Filters.ContentType = SearchResultsContentType.Vector And request.Filters.ContentType <> SearchResultsContentType.All)
                AppendParameter(strBuild, "search_parameters[filters][offensive:2]", "1", Function() request.Filters.Offensive)
                AppendParameter(strBuild, "search_parameters[filters][isolated:on]", "1", Function() request.Filters.IsolatedContents)
                AppendParameter(strBuild, "search_parameters[filters][panoramic:on]", "1", Function() request.Filters.PanoramicImages)
                AppendParameter(strBuild, "search_parameters[filters][licence_E:on]", "1", Function() request.Filters.Licenses = SearchResultsLicense.E And request.Filters.Licenses <> SearchResultsLicense.All)
                AppendParameter(strBuild, "search_parameters[filters][license_L:on]", "1", Function() request.Filters.Licenses = SearchResultsLicense.L And request.Filters.Licenses <> SearchResultsLicense.All)
                AppendParameter(strBuild, "search_parameters[filters][license_V_HD1080:on]", "1", Function() request.Filters.Licenses = SearchResultsLicense.VHD1080 And request.Filters.Licenses <> SearchResultsLicense.All)
                AppendParameter(strBuild, "search_parameters[filters][license_V_HD720:on]", "1", Function() request.Filters.Licenses = SearchResultsLicense.VHD720 And request.Filters.Licenses <> SearchResultsLicense.All)
                AppendParameter(strBuild, "search_parameters[filters][license_X:on]", "1", Function() request.Filters.Licenses = SearchResultsLicense.X And request.Filters.Licenses <> SearchResultsLicense.All)
                AppendParameter(strBuild, "search_parameters[filters][license_XL:on]", "1", Function() request.Filters.Licenses = SearchResultsLicense.XL And request.Filters.Licenses <> SearchResultsLicense.All)
                AppendParameter(strBuild, "search_parameters[filters][license_XXL:on]", "1", Function() request.Filters.Licenses = SearchResultsLicense.XXL And request.Filters.Licenses <> SearchResultsLicense.All)
                AppendParameter(strBuild, "search_parameters[filters][orientation]", "horizontal", Function() request.Filters.Orientation = SearchOrientation.Horizontal)
                AppendParameter(strBuild, "search_parameters[filters][orientation]", "vertical", Function() request.Filters.Orientation = SearchOrientation.Vertical)
                AppendParameter(strBuild, "search_parameters[filters][age]", "1m", Function() request.Filters.Age = SearchResultsAge.OneMonth)
                AppendParameter(strBuild, "search_parameters[filters][age]", "1w", Function() request.Filters.Age = SearchResultsAge.OneWeek)
                AppendParameter(strBuild, "search_parameters[filters][age]", "1y", Function() request.Filters.Age = SearchResultsAge.OneYear)
                AppendParameter(strBuild, "search_parameters[filters][age]", "6m", Function() request.Filters.Age = SearchResultsAge.SixMonths)
                AppendParameter(strBuild, "search_parameters[filters][age]", "2y", Function() request.Filters.Age = SearchResultsAge.TwoYears)
                AppendParameter(strBuild, "search_parameters[filters][video_duration]", "0", Function() request.Filters.VideoDuration = SearchVideoDuration.From0To10Seconds)
                AppendParameter(strBuild, "search_parameters[filters][video_duration]", "10", Function() request.Filters.VideoDuration = SearchVideoDuration.From10To20Seconds)
                AppendParameter(strBuild, "search_parameters[filters][video_duration]", "20", Function() request.Filters.VideoDuration = SearchVideoDuration.From20To30Seconds)
                AppendParameter(strBuild, "search_parameters[filters][video_duration]", "30", Function() request.Filters.VideoDuration = SearchVideoDuration.MoreThan30Seconds)
                AppendParameter(strBuild, "search_parameters[filters][max_price_xs]", "1", Function() request.Filters.MaxXSPrice = SearchXSPrice.EqualsTo1)
                AppendParameter(strBuild, "search_parameters[filters][max_price_xs]", "2", Function() request.Filters.MaxXSPrice = SearchXSPrice.LessThan2)
                AppendParameter(strBuild, "search_parameters[filters][max_price_x]", Function() request.Filters.MaxXPrice.Value, Function() request.Filters.MaxXPrice.HasValue)
            End If

            Return strBuild.ToString()
        End Function

        Public Shared Function GetMediaDataUrl(ByVal mediaId As Integer,
                                                 ByVal languageId As Integer,
                                                 ByVal thumbnailSize As SearchThumbnailSize) As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("media/getMediaData?")
            AppendParameter(strBuild, "id", mediaId)
            AppendParameter(strBuild, "thumbnail_size", FTTK.Entities.Rest.SearchThumbnailSize.FromEntitySize(thumbnailSize))
            AppendParameter(strBuild, "language_id", languageId)
            Return strBuild.ToString()
        End Function

        Public Shared Function GetBulkMediaDataUrl(ByVal mediaId As Integer,
                                                 ByVal languageId As Integer,
                                                 ByVal thumbnailSize As SearchThumbnailSize) As String

            Return GetBulkMediaDataUrl({mediaId}, languageId, thumbnailSize)
        End Function

        Public Shared Function GetBulkMediaDataUrl(ByVal mediaIds As IEnumerable(Of Integer),
                                                 ByVal languageId As Integer,
                                                 ByVal thumbnailSize As SearchThumbnailSize) As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("media/getBulkMediaData?")
            For Each mediaId In mediaIds
                AppendParameter(strBuild, "ids%5B%5D", mediaId)
            Next
            AppendParameter(strBuild, "thumbnail_size", FTTK.Entities.Rest.SearchThumbnailSize.FromEntitySize(thumbnailSize))
            AppendParameter(strBuild, "language_id", languageId)
            Return strBuild.ToString()
        End Function

        Public Shared Function GetMediaGalleriesUrl(ByVal mediaId As Integer,
                                                ByVal languageId As Integer,
                                                ByVal thumbnailSize As SearchThumbnailSize) As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("media/getMediaGalleries?")
            AppendParameter(strBuild, "id", mediaId)
            AppendParameter(strBuild, "thumbnail_size", FTTK.Entities.Rest.SearchThumbnailSize.FromEntitySize(thumbnailSize))
            AppendParameter(strBuild, "language_id", languageId)
            Return strBuild.ToString()
        End Function

        Public Shared Function GetMediaCompUrl(ByVal mediaId As Integer) As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("media/getMediaComp?")
            AppendParameter(strBuild, "id", mediaId)
            Return strBuild.ToString()
        End Function

        Public Shared Function GetLoginUserUrl() As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("user/loginUser")
            Return strBuild.ToString()
        End Function

        Public Shared Function GetLogoutUserUrl() As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("user/logoutUser")
            Return strBuild.ToString()
        End Function

        Public Shared Function GetUserDataUrl() As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("user/getUserData")
            Return strBuild.ToString()
        End Function

        Public Shared Function GetUserAdvancedStatsUrl(ByVal type As UserStatsType,
                                                     ByVal range As UserStatsTimeRange,
                                                     ByVal period As UserStatsDatePeriod?,
                                                     ByVal startDate As Date?,
                                                     ByVal endDate As Date?) As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("user/getUserAdvancedStats?")
            AppendParameter(strBuild, "type", UserStatsTypes.GetTypeString(type))
            AppendParameter(strBuild, "time_range", UserStatsTimeRanges.GetString(range))
            AppendParameter(strBuild, "easy_date_period", Function() UserStatsDatePeriods.GetString(period.Value), Function() period.HasValue)
            AppendParameter(strBuild, "start_date", Function() startDate.Value.ToString("yyyy-MM-dd"), Function() startDate.HasValue)
            AppendParameter(strBuild, "end_date", Function() endDate.Value.ToString("yyyy-MM-dd"), Function() endDate.HasValue)
            Return strBuild.ToString()
        End Function

        Public Shared Function GetUserStatsUrl() As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("user/getUserStats")
            Return strBuild.ToString()
        End Function

        Public Shared Function GetUserGalleriesUrl() As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("user/getUserGalleries")
            Return strBuild.ToString()
        End Function

        Public Shared Function GetUserGalleryMediasUrl(pageNumber As Integer,
                                                       mediaPerPage As Integer,
                                                       thumbnailSize As SearchThumbnailSize,
                                                       galleryId As String,
                                                       detailLevel As SearchDetailLevel) As String
            Dim firstAppend As Boolean = True
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("user/getUserGalleryMedias?")
            AppendParameter(strBuild, "id", galleryId, Function() Not String.IsNullOrEmpty(galleryId))
            AppendParameter(strBuild, "page", pageNumber, Function() pageNumber > 1)
            AppendParameter(strBuild, "nb_per_page", mediaPerPage, Function() mediaPerPage <> 32)
            AppendParameter(strBuild, "thumbnail_size", FTTK.Entities.Rest.SearchThumbnailSize.FromEntitySize(thumbnailSize), Function() thumbnailSize <> SearchThumbnailSize.Size110)
            AppendParameter(strBuild, "detail_level", 1, Function() detailLevel <> SearchDetailLevel.Base)
            Dim retval = strBuild.ToString()
            If retval.EndsWith("?") Then
                Return retval.Substring(0, retval.Length - 1)
            Else
                Return retval
            End If
        End Function

        Public Shared Function CreateUserGalleryUrl() As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("user/createUserGallery")
            Return strBuild.ToString()
        End Function

        Public Shared Function DeleteUserGalleryUrl() As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("user/deleteUserGallery")
            Return strBuild.ToString()
        End Function

        Public Shared Function AddToUserGalleryUrl() As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("user/addToUserGallery")
            Return strBuild.ToString()
        End Function

        Public Shared Function RemoveFromUserGalleryUrl() As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("user/removeFromUserGallery")
            Return strBuild.ToString()
        End Function


        Public Shared Function MoveUpMediaInUserGalleryUrl() As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("user/moveUpMediaInUserGallery")
            Return strBuild.ToString()
        End Function


        Public Shared Function MoveDownMediaInUserGalleryUrl() As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("user/moveDownMediaInUserGallery")
            Return strBuild.ToString()
        End Function


        Public Shared Function MoveMediaToTopInUserGalleryUrl() As String
            Dim strBuild As New StringBuilder
            strBuild.Append(_BaseUrl)
            strBuild.Append("user/moveMediaToTopInUserGallery")
            Return strBuild.ToString()
        End Function

#Region "Private Methods"
        Private Shared Sub AppendParameter(strBuild As StringBuilder, parameterName As String, parameterValue As Object)
            If Not strBuild.ToString.EndsWith("?") Then
                strBuild.Append("&")
            End If
            strBuild.AppendFormat("{0}={1}", parameterName, parameterValue)
        End Sub

        Private Shared Sub AppendParameter(strBuild As StringBuilder, parameterName As String, parameterValue As Object, checkLambda As Func(Of Boolean))
            If checkLambda.Invoke() Then
                If Not strBuild.ToString.EndsWith("?") Then
                    strBuild.Append("&")
                End If
                strBuild.AppendFormat("{0}={1}", parameterName, parameterValue)
            End If
        End Sub

        Private Shared Sub AppendParameter(strBuild As StringBuilder, parameterName As String, parameterLambda As Func(Of Object))
            If Not strBuild.ToString.EndsWith("?") Then
                strBuild.Append("&")
            End If
            strBuild.AppendFormat("{0}={1}", parameterName, parameterLambda.Invoke())
        End Sub

        Private Shared Sub AppendParameter(strBuild As StringBuilder, parameterName As String, parameterLambda As Func(Of Object), checkLambda As Func(Of Boolean))
            If checkLambda.Invoke() Then
                If Not strBuild.ToString.EndsWith("?") Then
                    strBuild.Append("&")
                End If
                strBuild.AppendFormat("{0}={1}", parameterName, parameterLambda.Invoke())
            End If
        End Sub
#End Region


    End Class
End Namespace